package com.zhuyuan.getaway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 张琳凯
 * @Description: TODO
 * @DateTime: 2025/2/9 16:19
 **/
@Configuration
public class DynamicRouteConfig {
//    @Autowired
//    private DiscoveryClient discoveryClient;
//
//    private final List<GroupedOpenApi> groups = new ArrayList<>();
//
//    @Bean
//    public List<GroupedOpenApi> groupedOpenApis() {
//        refreshGroups();
//        return groups;
//    }
//    public void refreshGroups() {
//        List<String> services = discoveryClient.getServices();
//        List<GroupedOpenApi> newGroups = services.stream()
//                .filter(service -> !service.equalsIgnoreCase("gateway"))  // 排除网关自身
//                .map(service -> GroupedOpenApi.builder()
//                        .group(service)
//                        .pathsToMatch("/" + service + "/**")
//                        .build())
//                .collect(Collectors.toList());
//        groups.clear();
//        groups.addAll(newGroups);
//    }
}
